package lol.clann.minecraft.springboot.plugin.bukkit.impl;

import lol.clann.minecraft.springboot.api.bukkit.utils.LocationUtils;
import lol.clann.minecraft.springboot.api.bukkit.utils.NBTUtils;
import lol.clann.minecraft.springboot.api.bukkit.utils.VectorUtils;
import net.minecraft.server.v1_8_R3.BlockPosition;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.Vector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author pyz
 * @date 2019/5/12 10:44 PM
 */
@Service
public class LocationUtilsImpl_all implements LocationUtils {
    @Autowired
    private NBTUtils nbtUtils;
    @Autowired
    private VectorUtils vectorUtils;

    @Override
    public <T> T newBlockPosition(int x, int y, int z) {
        return (T) new BlockPosition(x, y, z);
    }

    @Override
    public <T> T toBlockPosition(Location location) {
        return newBlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    }

    @Override
    public Location toLocation(World world, Object blockPosition) {
        BlockPosition bp = (BlockPosition) blockPosition;
        return new Location(world, bp.getX(), bp.getY(), bp.getZ());
    }

    @Override
    public Object toNBTTagCompound(Location location) {
        Object tag = nbtUtils.newNBTTagCompound();
        nbtUtils.setValue(tag, "world", location.getWorld().getName());
        nbtUtils.setValue(tag, "x", location.getX());
        nbtUtils.setValue(tag, "y", location.getY());
        nbtUtils.setValue(tag, "z", location.getZ());
        nbtUtils.setValue(tag, "pitch", location.getPitch());
        nbtUtils.setValue(tag, "yaw", location.getYaw());
        return tag;
    }

    @Override
    public void forward(Location now, double distance) {
        forward(now, now.getDirection(), distance);
    }

    @Override
    public void forward(Location now, Vector direction, double distance) {
        vectorUtils.adjust(direction, distance);
        now.add(direction);
    }

}
